<template>
    <el-dialog
            :title="title"
            :visible.sync="isVisible"
            :width="width"
            :before-close="handleClose"
            :lock-scroll="true"
            :close-on-click-modal="false"
            custom-class="pub_dialog"
            @open="getData"
    >
        <VForm
                ref="vformComponents"
                :form="formData.form"
                :options="formData.options"
                :rules="formData.rules"
                :labelWidth="formData.labelWidth"
                :rowspan="rowspan"
        ></VForm>

      <div class="formTitle-root" v-if="formData1.form.state==1 || openType=='edit'">
        <div class="form-title-p">回复信息</div>
      </div>

      <VForm
        v-show="formData1.form.state==1 || openType=='edit'"
        ref="vformComponents1"
        :form="formData1.form"
        :options="formData1.options"
        :rules="formData1.rules"
        :labelWidth="formData1.labelWidth"
        :rowspan="rowspan"
      ></VForm>

        <span slot="footer" class="dialog-footer">
      <el-button @click="cancel">取 消</el-button>
      <el-button type="primary" @click="sure" v-if="formData1.form.state==0 && openType=='edit'">确 定</el-button>
    </span>
    </el-dialog>
</template>

<script>
    import { cityTargetComplaintDetail,
            cityTargetComplaintUpdate,
            cityTargetComplaintAdd,
            cityTargetComplaintReply
    } from '@/api/risk/cityTargetComplaint'
    import VForm from "@/components/VForm/index";
    export default {
        components: {
            VForm,
        },
        props: {
            width: {
                type: String,
                default: "70%",
            },
            title: {
                type: String,
                default: "详情",
            },
            id:{},
            openType:{
                type:String,
                default:"add"
            },
            dialogVisible: {
                type: Boolean,
                default: false
            }
        },
        watch: {
            'width': {
                handler(newVal, oldVal) {
                    console.log('newVal===',newVal, oldVal)
                    let w = this.width.split('%')[0]
                    if(w>=60){
                        this.rowspan = 2
                    }else{
                        this.rowspan = 1
                    }
                },
                deep: true,
                immediate: true
            },
            'typeOptions':{
                handler(newVal, oldVal) {
                    this.formData.options.forEach(t=>{
                        if(t.key=='type'){
                        t.option.data = newVal
                    }
                })
                },
                deep: true,
                immediate: true
            },
          'openType':{
            handler(newVal, oldVal) {
              this.formData1.options.forEach(t=>{
                 t.disabled = newVal == 'edit' ? false : true
              })
            },
            deep: true,
            immediate: true
          }
        },
        computed: {
            isVisible: {
                get() {
                    return this.dialogVisible
                },
                set() {
                    this.close()
                }
            }
        },
        data() {
            return {
                rowspan:1,
                dict:{},
                formData1:{
                  form:{
                    // state:"",
                    replyContent:"",
                  },
                  options:[
                    // {
                    //   title: "回复状态",
                    //   key: "state",
                    //   type: "select",
                    //   multiple:false,
                    //   option: {
                    //     data: [{id:0,label:"待回复",value:0},{id:1,label:"已回复", value:1}],
                    //     props: {
                    //       key: "id", // 指定key对应data的字段
                    //       label: "label", // 指定label对应data的字段
                    //       value: "value", // 指定value对应data的字段
                    //     },
                    //   },
                    // },
                    {
                      title: "回复内容",
                      key: "replyContent",
                      type: "textarea",
                      columnWidth:'w100',
                    }],
                  rules: {
                    state :[
                      {required: true, message: '请选择', trigger: 'change'},
                    ],
                    replyContent :[
                      {required: true, message: '请输入', trigger: 'blur'},
                      {max:128, message: '长度最大为128个字符', trigger: 'blur'}
                    ],
                  },
                  labelWidth: "140px",
                },
                formData: {
                    form: {
                            type:"",
                            title:"",
                            complaintContent:"",
                            file:[],
                        },
                    options: [
                                {
                                    title: "类型",
                                    key: "type",
                                    type: "select",
                                    multiple:false,
                                    option: {
                                        data: this.typeOptions,
                                        props: {
                                            key: "id", // 指定key对应data的字段
                                            label: "label", // 指定label对应data的字段
                                            value: "value", // 指定value对应data的字段
                                        },
                                    },
                                    disabled:true
                                },
                                   {
                                       title: "标题",
                                       key: "title",
                                       type: "text",
                                       disabled:true
                                   },
                                    {
                                      title: "提交时间",
                                      key: "createTime",
                                      type: "datetime",
                                      disabled:true
                                    },
                                   {
                                       title: "投诉内容",
                                       key: "complaintContent",
                                       type: "textarea",
                                       columnWidth:'w100',
                                       disabled:true
                                   },
                                   {
                                       title: "附件",
                                       key: "file",
                                       type: "file",
                                       fileType:"file",
                                       listType:"text",
                                       uploadTextTip:"",
                                       disabled:true
                                   },

                    ],
                    rules: {
                    },
                    labelWidth: "140px",
                },
            // 字典
                typeOptions:[],
            };
        },
        methods: {
            submitFormat(){
                   this.formData.form.file = JSON.stringify(this.formData.form.file)
            },
            loadFormat(){
                    this.formData.form.file  = this.formData.form.file ? JSON.parse(this.formData.form.file) : []
            },
            getCheckBoxData(value,option,type){
                if(type=="submit"){
                    if(value.length==0){
                        return null
                    }
                    let temp = [];
                    value.forEach(t=>{
                        let item = option.find(x=> x.label == t)
                    if(item!=undefined){
                        temp.push(item.value)
                    }
                })
                    return temp.join(",")
                }else {
                    if(!value){
                        return []
                    }else {
                        let data = value.split(",")
                        let temp = []
                        data.forEach(t=>{
                            let item = option.find(x=> x.value == t)
                        if(item!=undefined){
                            temp.push(item.label)
                        }
                    })
                        return temp
                    }
                }
            },
            sure(){
                console.log('sure===',this.formData.form)
                this.$refs.vformComponents1.$refs.form.validate().then(res=>{
                  this.formData1.form.id = this.formData.form.id
                    cityTargetComplaintReply(this.formData1.form).then(re=>{
                      if(re.code==200){
                        this.$message({
                          message: this.$t('tips.updateSuccess'),
                          type: 'success'
                        })
                        this.closeSuccess()
                      }else{
                        this.$message({
                          message: this.$t('tips.systemException'),
                          type: 'error'
                        })}
                    });
                })
            },
            add(){
                this.formData.form.id = null
                cityTargetComplaintAdd(this.formData.form).then(res=>{
                    if(res.code==200){
                    this.$message({
                        message: this.$t('tips.createSuccess'),
                        type: 'success'
                    })
                    this.closeSuccess()
                    }else{
                    this.$message({
                        message: this.$t('tips.systemException'),
                        type: 'error'
                    })
                    }
                })
            },
            update(){
                   cityTargetComplaintUpdate(this.formData.form).then(res=>{
                        if(res.code==200){
                        this.$message({
                             message: this.$t('tips.updateSuccess'),
                            type: 'success'
                        })
                        this.closeSuccess()
                      }else{
                        this.$message({
                            message: this.$t('tips.systemException'),
                            type: 'error'
                        })
                     }
                })
            },
            cancel(){
                this.close()
            },
            handleClose(done) {
                done()
                this.close()
            },
            closeSuccess(){
                this.$refs.vformComponents.$refs.form.resetFields();
                this.$refs.vformComponents1.$refs.form.resetFields();
                this.$emit('success')
            },
            close(){
                this.$refs.vformComponents.$refs.form.resetFields();
                this.$refs.vformComponents1.$refs.form.resetFields();
                this.$emit('close')
            },
            getData(){
              this.typeOptions = []
              this.$commonJs.filterDataDict("cityTargetComplaintType", this.typeOptions)
                if(this.openType!="add"){
                    cityTargetComplaintDetail(this.id).then(res=>{
                        if(res.code==200){
                            this.formData.form = res.data
                            this.formData1.form.state = res.data.state,
                            this.formData1.form.replyContent = res.data.replyContent,
                            this.loadFormat()
                         }else{
                            this.$message({
                                message: this.$t('tips.systemException'),
                                type: 'error'
                            })
                        }
                    })
                }
            }
        },
    };
</script>

<style lang="scss" scoped>
  @import "@/styles/element-variables.scss";
  .formTitle-root{
    padding: 10px 0;
    border-bottom: 1px solid #EFEFEF;
    font-size: 16px;
    position: relative;
    margin-bottom: 5px;
    .form-title-p{
      padding-left: 10px;
      border-left: 4px solid $--color-primary;
    }
  }
</style>
